﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PSTUTemplate.Models.DBObject;

namespace PSTUTemplate.Models.Object
{
    public partial class ExamResultBLL 
    {
        #region Constructors, Dependency and Partial Method Declaration

        PSTUDBEntities _DatabaseContext = new PSTUDBEntities();

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public ExamResult GetExamResultById(int id)
        {
            //Validate Input

            return (_DatabaseContext.ExamResults.FirstOrDefault(examResult => examResult.Id == id));
        }
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
        public List<ExamResult> GetAllExamResults()
        {
            return _DatabaseContext.ExamResults.ToList();
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
        public List<ExamResult> GetAllExamResultsPaged(string orderBy = default(string), int startRowIndex = default(int), int maximumRows = default(int))
        {
            if (string.IsNullOrEmpty(orderBy))
                orderBy = "Id";

            if (startRowIndex < 0)
                throw (new ArgumentOutOfRangeException("startRowIndex"));

            if (maximumRows < 0)
                throw (new ArgumentOutOfRangeException("maximumRows"));

            return (
                    from examResult in
                        _DatabaseContext.ExamResults

                    select examResult
                    )
                    .Skip(startRowIndex)
                    .Take(maximumRows)
                    .ToList();
        }

        public int GetTotalCountForAllExamResults(string orderBy = default(string), int startRowIndex = default(int), int maximumRows = default(int))
        {
            return _DatabaseContext.ExamResults.Count();
        }

        #endregion

        #region Persistence (Create, Update, Delete) Methods

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public virtual int CreateNewExamResult(ExamResult newExamResult)
        {
            // Validate Parameters 
            if (newExamResult == null)
                throw (new ArgumentNullException("newExamResult"));

            // Apply business rules
            _DatabaseContext.ExamResults.AddObject(newExamResult);
            int numberOfAffectedRows = _DatabaseContext.SaveChanges();

            // Apply business workflow
            return newExamResult.Id;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public void UpdateExamResult(ExamResult updatedExamResult)
        {
            _DatabaseContext.ObjectStateManager.ChangeObjectState(updatedExamResult, System.Data.EntityState.Modified);//this line makes the code un-testable!
            int numberOfAffectedRows = _DatabaseContext.SaveChanges();

        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public void DeleteExamResult(ExamResult examResultToBeDeleted)
        {
            //Validate Input
            if (examResultToBeDeleted == null)
                throw (new ArgumentNullException("examResultToBeDeleted"));
            _DatabaseContext.ExamResults.DeleteObject(examResultToBeDeleted);
            int numberOfAffectedRows = _DatabaseContext.SaveChanges();
        }

        public void DeleteExamResults(List<int> idsToDelete)
        {
            //Validate Input


            List<ExamResult> examResultsToBeDeleted = new List<ExamResult>();

            foreach (int id in idsToDelete)
            {
                ExamResult examResult = new ExamResult { Id = id };
                _DatabaseContext.ExamResults.Attach(examResult);
                _DatabaseContext.ExamResults.DeleteObject(examResult);
                examResultsToBeDeleted.Add(examResult);

            }

            int numberOfAffectedRows = _DatabaseContext.SaveChanges();


        }
    }
        #endregion
}
